﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Business;

namespace Actuals.Business
{
    public class BusinessActualFigures : IBusinessActualFigures
    {
        IBusinessProject _businessProject;
        IBusinessRelease _businessRelease;
        IBusinessTeamMember _businessTeamMember;
        IBusinessPublicHoliday _businessPublicHoliday;
        IBusinessEmployeeLeave _businessEmployeeLeave;
        IBusinessNoOfDays _businessNoOfDays;
        IBusinessPartTimeDetail _businessPartTimeDetails;

        public BusinessActualFigures()
        {
            _businessProject = new BusinessProject();
            _businessRelease = new BusinessRelease();
            _businessTeamMember = new BusinessTeamMember();
            _businessPublicHoliday = new BusinessPublicHoliday();
            _businessEmployeeLeave = new BusinessEmployeeLeave();
            _businessNoOfDays = new BusinessNoOfDays();
            _businessPartTimeDetails = new BusinessPartTimeDetail();
        }


        public IList<IActualFigures> GetActualFiguresForFullTimers(DateTime fromDate, DateTime toDate, int projectId, int releaseId)
        {

            
            IProject project = _businessProject.GetProject(projectId);
            IRelease release = _businessRelease.GetRelease(projectId, releaseId);

            IList<ITeamMember> teamMembers = _businessTeamMember.GetTeamMembers(project.TeamId);

            //IList<IPublicHoliday> publicHolidays = _businessPublicHoliday.GetPublicHolidays(fromDate, toDate);

            double _noOfBDays = _businessNoOfDays.CaculateNoOfBusinessDays(fromDate, toDate);

            IList<IActualFigures> actualFigureses = new List<IActualFigures>();

            foreach (ITeamMember teamMember in teamMembers)
            {


                IBusinessEmployee businessEmployee = new BusinessEmployee();
                IEmployee employee = businessEmployee.GetEmployee(teamMember.EmployeeId);

                if ((teamMember.WorkMode == "F") && (employee.RoleId!=2))
                {
                    IActualFigures actualFigures = new ActualFigures();
                    
                 
                    actualFigures.NickName = employee.NickName;
                    actualFigures.EmployeeId = teamMember.EmployeeId;

                    actualFigures.WorkMode = "F";
                    actualFigures.HoursPerDay = teamMember.NoOfHoursPerDay;
                    actualFigures.NoOfDays = _noOfBDays;
                    actualFigures.TotalHours = actualFigures.HoursPerDay * actualFigures.NoOfDays;
                    actualFigureses.Add(actualFigures);
                }
            }

            IBusinessTeamEmpJoinLeave businessTeamEmpJoinLeave = new BusinessTeamEmpJoinLeave();
            IList<IEmpTeamJoinLeave> empTeamJoinLeaves = businessTeamEmpJoinLeave.
                GetDetails(project.TeamId, fromDate, toDate);

            foreach (IEmpTeamJoinLeave empTeamJoinLeave in empTeamJoinLeaves)
            {

                IBusinessEmployee businessEmployee = new BusinessEmployee();
                IEmployee employee = businessEmployee.GetEmployee(empTeamJoinLeave.EmployeeId);

                IActualFigures actualFigures = new ActualFigures();

                if (employee.RoleId != 2)
                {
                    actualFigures.NickName = employee.NickName;
                    actualFigures.EmployeeId = empTeamJoinLeave.EmployeeId;
                    actualFigures.WorkMode = "F";
                    actualFigures.HoursPerDay = empTeamJoinLeave.NoOfHrs;
                    DateTime _startDate = (empTeamJoinLeave.StartDate >= fromDate) ? empTeamJoinLeave.StartDate : fromDate;
                    DateTime _toDate = (empTeamJoinLeave.Enddate <= toDate) ? empTeamJoinLeave.Enddate : toDate;                  
                    actualFigures.NoOfDays = _businessNoOfDays.CaculateNoOfBusinessDays(_startDate, _toDate);
                    actualFigures.TotalHours = actualFigures.HoursPerDay * actualFigures.NoOfDays;
                    actualFigureses.Add(actualFigures);
                }
            }

            return actualFigureses;
            

        }

        public IList<IActualFiguresPartTime> GetActualFiguresForPartTimers(DateTime fromDate, DateTime toDate, int projectId, int releaseId)
        {


            IProject project = _businessProject.GetProject(projectId);
            IRelease release = _businessRelease.GetRelease(projectId, releaseId);

            IList<ITeamMember> teamMembers = _businessTeamMember.GetTeamMembers(project.TeamId);

            IList<IActualFiguresPartTime> actualFigureses = new List<IActualFiguresPartTime>();

            foreach (ITeamMember teamMember in teamMembers)
            {
              

                if (teamMember.WorkMode == "P")
                {
                    
                    IList<IPartTimeDetails> partTimeDetailses = _businessPartTimeDetails.GetPartTimeDetails(fromDate, toDate, teamMember.EmployeeId);
                    foreach (IPartTimeDetails partTimeDetail in partTimeDetailses)
                    {
                        IActualFiguresPartTime actualFiguresPartTime = new ActualFiguresPartTime();
                        IBusinessEmployee businessEmployee = new BusinessEmployee();
                        IEmployee employee = businessEmployee.GetEmployee(teamMember.EmployeeId);
                        actualFiguresPartTime.WorkMode = "P";
                        actualFiguresPartTime.NickName = employee.NickName;
                        actualFiguresPartTime.EmployeeId = teamMember.EmployeeId;
                        actualFiguresPartTime.WorkDate = partTimeDetail.WorkDate;
                        actualFiguresPartTime.HoursPerDay = partTimeDetail.NoOfHours;
                        actualFiguresPartTime.TotalHours = actualFiguresPartTime.HoursPerDay;
                        actualFigureses.Add(actualFiguresPartTime);
                    }
                    
                }


                
            }

            return actualFigureses;


        }

        public double GetTotalHours(IList<IActualFigures> actualFigureses)
        {
            return actualFigureses.ToList().Sum(x => x.TotalHours);
        }

        public int GetTotalHoursPartTime(IList<IActualFiguresPartTime> actualFigureses)
        {
            return actualFigureses.ToList().Sum(x => x.HoursPerDay);
        }
    }
}
